package crawler.biyadi;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zql.entity.AgencyEntity;

import dao.AgencyDao;
import util.HttpConnectionPost;
import util.MybatisTool;
import util.TianYanCha;

public class BiYaDiCrawler {
	public List<AgencyEntity> getAgency() {
		List<AgencyEntity> list = new ArrayList<AgencyEntity>();
		try {
			//获取省市的name
			String getPro = "http://www.bydauto.com.cn/ajax.php?act=getpro&inajax=1";
			Document proDoc = Jsoup.connect(getPro).get();
			Elements proOption = proDoc.getElementsByTag("option");
			for(int i=1;i<proOption.size();i++) {
				String proName = proOption.get(i).text();
				
				//根据省名作为id,发送post请求,获取省中的市
				String proUrl = "http://www.bydauto.com.cn/ajax.php?act=getcity&inajax=1";
				Document cityDoc = Jsoup.connect(proUrl).data("pid", proName).data("carid", "0").post();
				Elements cities = cityDoc.getElementsByTag("option");
				for(int j=0;j<cities.size();j++) {
					String cityName = cities.get(j).text();
					
					//根据市名作为id,发送post请求,获取经销商字符串
					String path = "http://www.bydauto.com.cn/ajax.php?act=getsellpoint&inajax=1";
					//测试得出,当汽车类型(carid)为0时,返回所有经销商.
					String param = "carid=0&showtype=json&cid="+cityName;
					String text = HttpConnectionPost.getJson(path, param);
					//把字符串解析成json
					JSONArray dealers = JSON.parseArray(text);
					for(int k=0;k<dealers.size();k++) {
						JSONObject dealer = dealers.getJSONObject(k);
						String name = dealer.getString("sjname");
						String sellTell = dealer.getString("phone");
						String address = dealer.getString("address");
						String lng = dealer.getString("jingdu");
						String lat = dealer.getString("weidu");
						List<String> shareholder = TianYanCha.getShareholder(name);
						String sControllingShareholder = shareholder.get(0);
						String sOtherShareholders = shareholder.get(1);
						
						AgencyEntity agency = new AgencyEntity();
						agency.setdCloseDate(null);
						agency.setdOpeningDate(null);
						agency.setdUpdateTime(new Timestamp(System.currentTimeMillis()));
						agency.setnBrandID(-1);
						agency.setsBrand("比亚迪");

						agency.setnDealerIDWeb(-1);
						agency.setnManufacturerID(-1);
						agency.setsManufacturer("比亚迪汽车");

						agency.setnState(1);
						agency.setsAddress(address);
						agency.setsCity(cityName);
						agency.setsCustomerServiceCall(null);
						agency.setsDealerName(name);
						agency.setsDealerType(null);
						agency.setsProvince(proName);
						agency.setsSaleCall(sellTell);
						agency.setsLongitude(lng);
						agency.setsLatitude(lat);
						agency.setsControllingShareholder(sControllingShareholder);
						agency.setsOtherShareholders(sOtherShareholders);
						list.add(agency);
						
					}
				}
			}
			
			
		} catch(Exception e) {
			throw new RuntimeException("爬虫异常", e);
		}
		
		return list;
	}
	public static void main(String[] args) {
		BiYaDiCrawler crawler = new BiYaDiCrawler();
		System.out.println("爬虫开始...");
		AgencyDao dao = new AgencyDao();
		List<AgencyEntity> agencys = crawler.getAgency();
		System.out.println("抓取完毕,正在存库");
		for(AgencyEntity agency : agencys) {
			MybatisTool.save(agency);
		}
		MybatisTool.close();
		System.out.println("请查看数据库");
		
	}

}
